package com.sbp.message.service;

import com.alibaba.fastjson.JSON;
import com.dap.utils.AsyncQueue;
import com.sbp.message.api.entity.Message;
import com.sbp.message.comm.EmailUtils;
import com.sbp.message.service.cs.MessageCSImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ErrorNotifyServiceImpl implements InitializingBean, AsyncQueue.AsyncQueueConsumer {
    private static final Logger logger = LoggerFactory.getLogger(ErrorNotifyServiceImpl.class);

    @Value("${app.env}")
    private String appEnv;
    @Value("${app.company.name}")
    private String appCompanyName;
    @Value("${notify.mobile.json}")
    private String notifyMobileJson;
    private List<String> notifyMobileList;
    @Value("${notify.email.json}")
    private String notifyEmailJson;
    private List<String> notifyEmailList;
    @Autowired
    private MessageCSImpl messageCSImpl;
    @Autowired
    private SmsServiceImpl smsServiceImpl;



    @Override
    public void afterPropertiesSet() throws Exception {
        notifyMobileList = JSON.parseArray(notifyMobileJson, String.class);
        notifyEmailList = JSON.parseArray(notifyEmailJson, String.class);
    }


    private void realNotify(String errorType, String errorMessage, String messageId) {
        Message message;
        try {
            message = messageCSImpl.selectById(messageId);
        } catch (Exception e) {
            logger.info("", e);
            return;
        }

        try {
            smsServiceImpl.sendNotifySms(errorMessage, messageId, message.getDestination(), appCompanyName, notifyMobileList);
        } catch (Exception e) {
            logger.info("", e);
        }

        try {
            EmailUtils.sendNotifyEmail(errorType, message, appCompanyName, notifyEmailList);
        } catch (Exception e) {
            logger.info("", e);
        }
    }




    private static class NotifyParam {
        public final String errorType;
        public final String errorMessage;
        public final String messageId;
        public NotifyParam(String errorType, String errorMessage, String messageId) {
            this.errorType = errorType;
            this.errorMessage = errorMessage;
            this.messageId = messageId;
        }
    }
    private AsyncQueue asyncQueue = new AsyncQueue();
    public void notify(String errorType, String errorMessage, String messageId) {
        if (!"product".equals(appEnv)) {
            logger.info("no need to notify, errorType={}, errorMessage={}, messageId={}", errorType, errorMessage, messageId);
            return;
        }

        NotifyParam notifyParam = new NotifyParam(errorType, errorMessage, messageId);
        asyncQueue.enqueueMessage(this, notifyParam, null);
    }
    @Override
    public void consumeAsyncQueueContent(Object o) {
        try {
            NotifyParam notifyParam = (NotifyParam) o;
            if (notifyParam != null) {
                realNotify(notifyParam.errorType, notifyParam.errorMessage, notifyParam.messageId);
            }
        } catch (Exception e) {
            logger.warn("", e);
        }
    }
}
